####################################################################################
########                       BlackBirdDaemon                               #######
########                       Scopel Emanuele                               #######
########                      escopel@alice.it                               #######
########                         license GPL3                                #######
####################################################################################

import ConfigParser, os, sys


class ConfigFileBuilder():

    def __init__(self, FileLocation = '', FileName = ''):
        """
        Inizializza i dizionari.
        -Un dizionario per ogni sezione del file di configurazione.
        -Ogni dizionario deve contenere una chiave section_name che identifica il nome della 
            sezione.
        -Ognidizionario deve essere contenuto nella SectionList.
        """
    	self.file = FileName
    	self.location = FileLocation
    	self.exit_status = False
    	self.Section1 = {
    		'section_name':'Client',
    		'ip' : '192.168.1.1',
    		'user' : 'user',
            'password' : 'password',
            'port' : '40200',
            'name' : 'client'
    	}
    	self.Section2 = {
    		'section_name' : 'FileEsclusi',
    		'prova1' : ''   		
    	}
    	self.Section3 = {
    		'section_name' : 'Local',
    		'prova1' : 'home'
    		
    	}
    	self.Section4 = {
    		'section_name' : 'Remote',
    		'prova1' : 'media'
    	}
        self.Section5 = {
    		'section_name' : 'Opzioni',
    		'prova1' : '-auv'
    	}
    	self.Section6 = {
    		'section_name' : 'Note',
    		'prova1' : 'ciao'
    	}
        self.Section7 = {
    		'section_name' : 'Sospesi',
    		'prova1' : 'False'
    	}

    	self.SectionList = [self.Section1, self.Section2, self.Section3, self.Section4,self.Section5, self.Section6, self.Section7]
    	    	
    def build(self):
        """
        Crea il file di configurazione usando i valori contenuti nei dizionari
        """
        config = ConfigParser.ConfigParser()
        	#open configuration file
    	fd = open(os.path.join(self.location, self.file), 'w')
    	for section in self.SectionList:
    		#create new section
    		config.add_section(section['section_name'])
    		for key in section.keys():
    			if key != 'section_name':
    				#add new option e related value
    				config.set(section['section_name'], key, section[key])
    	#Write config file
    	config.write(fd)
    	#close config file
    	fd.close()


#DIR = '/home/emanuele/Progetti/BoaProgetti/BlackBirdDaemon'
DIR = 'D:\Documents and Settings\A127027\Documenti\\tmp\BlackBirdDaemon'
FILE = 'config.cfg'
cfg = ConfigFileBuilder(DIR, FILE)
cfg.build()

